<template>
  <div class="register-bg">
    <div class="register-container">
      <el-card class="register-card">
        <div class="register-title">注册新账户</div>
        <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
          <el-form-item label="账号" prop="account">
            <el-input v-model="form.account" placeholder="请输入账号" />
          </el-form-item>
          <el-form-item label="真实姓名" prop="username">
            <el-input v-model="form.username" placeholder="请输入姓名" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password />
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input v-model="form.confirmPassword" type="password" placeholder="请再次输入密码" show-password />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" @click="onRegister" style="width:100%">注册</el-button>
          </el-form-item>
        </el-form>
        <div class="register-actions">
          <el-button type="text" class="action-btn" @click="goLogin">返回登录</el-button>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import axios from 'axios';

const router = useRouter();
const formRef = ref();
const loading = ref(false);

const form = ref({
  account: '',
  username: '',
  password: '',
  confirmPassword: '',
});

const rules = {
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' },
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: (rule, value, callback) => {
        if (value !== form.value.password) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      }, trigger: 'blur' },
  ],
};

const onRegister = async () => {
  try {
    await formRef.value.validate();
    loading.value = true;
    const { data } = await axios.post('http://121.43.176.9:8090/api/user/register', {
      account: form.value.account,
      username: form.value.username,
      password: form.value.password,
    });
    if (data.code === 200) {
      ElMessage.success('注册成功，请登录');
      router.replace({ path: '/' });
    } else {
      ElMessage.error(data.msg || '注册失败');
    }
  } catch (err) {
    if (err && err.message) {
      ElMessage.error(err.message);
    }
  } finally {
    loading.value = false;
  }
};

const goLogin = () => {
  router.replace({ path: '/' });
};
</script>

<style scoped>
.register-bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  background: #fff4f4;
  display: flex;
  align-items: center;
  justify-content: center;
}
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}
.register-card {
  width: 400px;
  box-shadow: 0 2px 16px rgba(229,57,53,0.08);
}
.register-title {
  font-size: 22px;
  font-weight: 600;
  color: #e53935;
  text-align: center;
  margin-bottom: 24px;
}
.register-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 8px;
}
.action-btn {
  color: #e53935;
  font-size: 15px;
  padding: 0 8px;
}
</style> 